import express from "express";
import pathToRegExp from "path-to-regexp";
const app = express();

app.get("/user/:id/:name/add", function (req, res, next) {
  res.send(req.params);
});

let paramsPath = "/user/:id/:name/add"; //  -> 正则 /user/(.+?)/(.+?)/add

let keys = [];
const regStr = paramsPath.replace(/:([^/]+)/g, function () {
  keys.push(arguments[1]);
  return "([^/]+)";
});
const reg = new RegExp(regStr);
const requestPath = "/user/1/jw/add";
let [, ...args] = requestPath.match(reg);
console.log(args, keys);
console.log(pathToRegExp(paramsPath));
app.listen(3000);
